﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Data;
using Ultima.Xna.Extensions;
using Ultima.Xna.Graphics.UI;
//using Ultima.Xna.Graphics.UI.Gumps;
//using Ultima.Xna.Graphics.UI.Presets;
using Ultima.Xna.Input;
using Ultima.Xna.Net;
using Ultima.Xna.Net.Packets;

namespace Ultima.Xna.SceneManagement
{
    public class LoginScene : BaseScene
    {
        INetworkService _networkService;
        Texture2D _background;

        UINode _dialogGump;
        UINode _loginGump;

        public LoginScene(Game game)
            : base(game)
        {
            _networkService = game.Services.GetService<INetworkService>(true);

            PacketHandlers.LoginRejection += new TypedPacketReceiveHandler(OnLoginRejection);
            PacketHandlers.ServerList += new TypedPacketReceiveHandler(OnServerList);
            PacketHandlers.CharacterListUpdate += new TypedPacketReceiveHandler(OnCharacterListUpdate);
            PacketHandlers.CharactersStartingLocations += new TypedPacketReceiveHandler(OnCharactersStartingLocations);
            PacketHandlers.LoginComplete += new TypedPacketReceiveHandler(OnLoginComplete);
            PacketHandlers.ServerRelay += new TypedPacketReceiveHandler(OnServerRelay);
            PacketHandlers.SupportedFeatures += new TypedPacketReceiveHandler(OnSupportedFeatures);
        }

        public override void Intitialize()
        {
            OnStatusUpdate(this, new StatusUpdateEventArgs("Loading Textures"));
            _background = Gumps.CreateTexture(GraphicsDevice, 0x500);

            OnProgressUpdate(this, new ProgressUpdateEventArgs(1, 2));
            OnStatusUpdate(this, new StatusUpdateEventArgs("Loading UI Components"));

            //UI.CreateInstance("testgump", true);
            //_loginGump = UI.CreateInstance("logingump", true);
            //_loginGump.Children.FindNodeByName<GumpLabel>("lblVersion", true).Text += ApplicationInfo.Version.ToString();
            //_loginGump.Children.FindNodeByName<GumpLabel>("lblVersionBackground", true).Text += ApplicationInfo.Version.ToString();
            //_loginGump.Children.FindNodeByName<GumpButton>("btnConnect", true).MouseClick += new EventHandler<MouseEventArgs>(OnConnectClick);

            OnProgressUpdate(this, new ProgressUpdateEventArgs(2, 2));

            base.Intitialize();
        }

        public void OnConnectClick(object sender, MouseEventArgs e)
        {
            _networkService.Connect("fds.bounceme.net", 2593);

            //if (_networkService.IsConnected)
            //{
            //    _networkService.Send(new SeedPacket(_networkService.ClientAddress, 6, 0, 5, 0));
            //    _networkService.Send(new LoginPacket(_loginGump.Children.FindNodeByName<GumpTextEntry>("txtAccountName", true).Text,
            //                                         _loginGump.Children.FindNodeByName<GumpTextEntry>("txtPassword", true).Text));
            //}

            //_loginGump.Visible = false;
            //_dialogGump = DialogGump.Show(Game, "Connecting to server...", new EventHandler<MouseEventArgs>(OnCancelConnecting));
        }

        private void OnCancelConnecting(object sender, MouseEventArgs e)
        {
            _networkService.Disconnect();
            //_loginGump.Visible = true;
        }

        private void OnCloseDialog(object sender, MouseEventArgs e)
        {
            _networkService.Disconnect();
            //_loginGump.Visible = true;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            PresentationParameters pp = GraphicsDevice.PresentationParameters;

            SpriteBatch.Begin();
            SpriteBatch.Draw(_background, new Rectangle(0, 0, pp.BackBufferWidth, pp.BackBufferHeight), Color.White);
            SpriteBatch.End();
        }

        private void OnSupportedFeatures(IRecvPacket packet)
        {

        }

        private void OnServerRelay(IRecvPacket packet)
        {

        }

        private void OnLoginComplete(IRecvPacket packet)
        {

        }

        private void OnCharactersStartingLocations(IRecvPacket packet)
        {

        }

        private void OnCharacterListUpdate(IRecvPacket packet)
        {

        }

        private void OnServerList(IRecvPacket packet)
        {
            //_dialogGump.Close();
        }

        private void OnLoginRejection(IRecvPacket packet)
        {
            LoginRejectionPacket p = (LoginRejectionPacket)packet;

            //_dialogGump.Close();
            //_dialogGump = DialogGump.Show(Game, p.Reason, new EventHandler<MouseEventArgs>(OnCancelConnecting));
        }
    }
}
